import React from 'react';
import {Provider, observer, inject} from 'mobx-react';
import {asyncComponent} from 'react-async-component';
import {withRouter, Switch, Route, Redirect} from 'react-router-dom';
import {Tools} from 'weaver-mobile';
import * as EmSDK from "../util/OdocEmSDK";
import * as pageTypes from "../constants/OdocPageType";
import {closeConfirmModal} from "../util/CommonUtil";

const getLocaleLabel = Tools.getLocaleLabel;

const coms = {
    OdocRequest: asyncComponent({       //公文流转
        resolve: () => import('./odocRequest/index')
    }),
    OdocWithDraw: asyncComponent({//撤回
        resolve: () => import('./withdraw/ordinary/index')
    }),
    OdocWithDrawManager: asyncComponent({//撤回
        resolve: () => import('./withdraw/manager/index')
    }),
    OdocUpdateOperator: asyncComponent({//加减签
        resolve: () => import("./updateOperator/index")
    }),
    OdocChangeOdocType: asyncComponent({//文种变更
        resolve: () => import("./odocTypeChangeDialog/index")
    }),
    OdocCusTab: asyncComponent({//自定义页签
        resolve: () => import("./odocCusTab/index")
    }),
    OdocAppendSign: asyncComponent({ // 追加意见
        resolve: () => import('./signlog/append')
    }),
    OdocAppendSignSelf: asyncComponent({ // 追加意见
        resolve: () => import('./signlog/append/indexSelf')
    }),
    OdocUpdateSign: asyncComponent({ // 修改意见
        resolve: () => import('./signlog/update')
    }),
    AdjustLog: asyncComponent(({
        resolve: () => import("./adjustLog/adjustLog")
    })),
    OdocSignApproval: asyncComponent({ // 签批
        resolve: () => import('./odocSignApproval/index.js')
    }),
    OdocSignApprovalHistory: asyncComponent({ // 签批
        resolve: () => import('./odocSignApproval/DocumentHistoryVersion')
    }),
}

@withRouter
@inject("odocComsStore")
@observer
export default class OdocComs4Export extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            loadLang: false
        };
    }

    componentWillUnmount() {
        const {odocComsStore} = this.props;
        const {onClose} = odocComsStore;
        onClose && typeof onClose == "function" && onClose();

        odocComsStore.resetStore();
    }

    setNavTitle(store, nextPathname) {
        const {title} = store;
        EmSDK.setOdocNavTitle(title, nextPathname);
    }

  /*  componentDidMount() {
        //加载多语言标签
        getLocaleLabel("odoc", () => {
            this.setState({loadLang: true})
        });
    }*/

    componentWillReceiveProps(nextProps, nextContext) {
        const location = this.props.location || {};
        const nextLocation = nextProps.location || {};
        const pathname = location.pathname || "";
        const nextPathname = nextLocation.pathname || "";

        if (pathname != nextPathname) {
            if (nextPathname.indexOf("/req/odocComs") != -1) {
                this.setNavTitle(nextProps.odocComsStore, nextPathname);
            }

            //关闭可能存在的，已弹出，但是未关闭的modal弹框
            closeConfirmModal();
        }
    }

    componentWillMount() {
        const {location, odocComsStore} = this.props;
        const {state = {}, callback, onClose, workflowStores} = location;
        const {comType, title} = state;
        const params = {
            urlParams: state,
            submitCallBack: callback,
            onClose,
            title,
            workflowStores,
            comType
        }
        odocComsStore.setPropValue(params);
        this.setNavTitle(odocComsStore);
    }

    render() {
        const {odocComsStore} = this.props;
        const {comType} = odocComsStore;
        const {
            OdocRequest, OdocWithDraw, OdocWithDrawManager, OdocUpdateOperator, OdocChangeOdocType, OdocCusTab,
            OdocAppendSign, OdocAppendSignSelf, OdocUpdateSign, AdjustLog, OdocSignApproval, OdocSignApprovalHistory
        } = coms;
        /*
        const {location = {}} = this.props;


        let OdocComs = null;
        switch (comType) {
            case pageTypes.ODOC_TRANSFER_DIALOG :
                OdocComs = <OdocRequest/>;
                break;
            case pageTypes.ODOC_WITHDRAW_DIALOG:
                OdocComs = <OdocWithDraw/>;
                break;
            case pageTypes.ODOC_WITHDRAW_MANAGER_DIALOG:
                OdocComs = <OdocWithDrawManager/>;
                break;
            case pageTypes.ODOC_UPDATEOPERATOR_DIALOG:
                OdocComs = <OdocUpdateOperator/>;
                break;
            case pageTypes.ODOC_CHANGE_ODOC_TYPE_DIALOG:
                OdocComs = <OdocChangeOdocType/>;
                break;
            case pageTypes.ODOC_CHANGE_ODOC_CUS_TAB:
                OdocComs = <OdocCusTab cusTabStore={odocComsStore}/>;
                break;
            case pageTypes.ODOC_APPEND_SIGNLOG:
                OdocComs = <OdocAppendSign/>;
                break;
            case pageTypes.ODOC_APPEND_SIGNLOG_SELF:
                OdocComs = <OdocAppendSignSelf/>;
                break;
            case pageTypes.ODOC_UPDATE_SIGNLOG:
                OdocComs = <OdocUpdateSign/>;
                break;
            case pageTypes.ODOC_ADJUST_LOG:
                OdocComs = <AdjustLog/>
                break;
            case pageTypes.ODOC_SIGN_APPROVAL:
                OdocComs = <OdocSignApproval/>;
                break;
            case pageTypes.ODOC_SIGN_APPROVAL_HISTORY:
                OdocComs = <OdocSignApprovalHistory/>;
                break;
            default:
                break;
        }
        */

        //return OdocComs == null ? <div/> : OdocComs

        return (
            <Switch>
                /**
                 * 公文流转
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_TRANSFER_DIALOG}`} component={OdocRequest}/>,

                /**
                 * 撤回
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_WITHDRAW_DIALOG}`} component={OdocWithDraw}/>,

                /**
                 * 撤回-管理员
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_WITHDRAW_MANAGER_DIALOG}`}
                       component={OdocWithDrawManager}/>,

                /**
                 * 加减签
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_UPDATEOPERATOR_DIALOG}`} component={OdocUpdateOperator}/>,

                /**
                 * 文种变更
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_CHANGE_ODOC_TYPE_DIALOG}`} component={OdocChangeOdocType}/>,

                /**
                 *  签字意见分组
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_CHANGE_ODOC_CUS_TAB}`} component={OdocCusTab}/>,

                /**
                 *  代录意见
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_APPEND_SIGNLOG}`} component={OdocAppendSign}/>,

                /**
                 * 追加意见
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_APPEND_SIGNLOG_SELF}`} component={OdocAppendSignSelf}/>,

                /**
                 * 修改意见
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_UPDATE_SIGNLOG}`} component={OdocUpdateSign}/>,

                /**
                 * 调整日志
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_ADJUST_LOG}`} component={AdjustLog}/>,

                /**
                 * 签批
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_SIGN_APPROVAL}`} component={OdocSignApproval}/>,

                /**
                 * 签批历史
                 */
                <Route path={`/req/odocComs/${pageTypes.ODOC_SIGN_APPROVAL_HISTORY}`}
                       component={OdocSignApprovalHistory}/>,

                /**
                 * 入口，跳转到具体的页面
                 */
                <Redirect exact path="/req/odocComs" to={{pathname: `/req/odocComs/${comType}`}}/>
            </Switch>
        );
    }
}